package j04zuoye;
/*
圆
定义一个圆类Circle, 包含圆心center(Point类型), 半径r, 包含如下方法:

计算圆的周长的方法perimeter,
计算圆的面积的方法area,
一个点Point是否在圆内部public boolean containsPoint(Point p);
当前圆(this)是否和另外一个圆相交, public boolean isIntersect(Circle c); 相交返回true, 不相交返回false
圆心center使用自定义的Point类作为类型
 */
class Point{
    double x;
    double y;

    public Point() {
    }

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}
class Circle{
    //圆心
    Point center;
    double r;

    public Circle() {
    }

    public Circle(Point center, double r) {
        this.center = center;
        this.r = r;
    }
    //检测点是否在圆内
    //p 目标点
    //return true在圆内，否则不在内部

    /**
     * 文档注释
     * 检测点是否在圆内
     * @param p 目标点
     * @return true在圆内，否则不在内部
     */
    public boolean containsPoint(Point p){
        double disX = p.x-center.x;
        double disY = p.y-center.y;
        double dis = Math.sqrt(disX*disX+disY*disY);
        return dis<r;
    }

    /**
     * 判断两个圆是否相交
     * @param c 目标圆
     * @return 相交true，否则false
     */
    public boolean isIntersect(Circle c){
        // 求圆心之间距离
        double disX = c.center.x - center.x;
        double disY = c.center.y - center.y;
        double disCenter = Math.sqrt(disX*disX+disY*disY);

        return disCenter<c.r+r && disCenter > (r-c.r>0?r-c.r:c.r-r);
    }

    //计算周长
    public double perimeter(){
        return 3.14*r*2;
    }
    //面积
    public double area(){
        return 3.14*r*r;
    }
}
public class Z1CircleTest {
    public static void main(String[] args) {
        //测试代码
        Circle c1 = new Circle();
        c1.center = new Point();
        c1.center.x = 4.0;
        c1.center.y = 5.0;
        c1.r = 10;

        System.out.println("c1.perimeter() = " + c1.perimeter());
        System.out.println("c1.area() = " + c1.area());
        System.out.println("====");
        Circle c2 = new Circle(
                new Point(4,5),
                20
        );
        System.out.println("c2.perimeter() = " + c2.perimeter());
        System.out.println("c2.area() = " + c2.area());

        System.out.println("====");
        Circle c0 = new Circle(
                new Point(0,0),
                10
        );
        Point px = new Point(20,0);
        System.out.println(c0.containsPoint(px));//false
        System.out.println(c0.containsPoint(
                new Point(5,0)
        ));//true

    }
}
